/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-18 16:36:29
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import {
  getEmissionOptions,
  getFactorOptions,
  getRelationList
} from '@/apis/system/config/relationship'
import { getMaterialListDrop } from '@/apis/system/config/material'
import type { GetRelationListParams } from '@/apis/system/config/model/relationshipModel'
import { emissionTypeEnum, CarbonInventoryStandardsMap } from '@/constants/enum'

import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useRelationshipList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetRelationListParams>({
    frameworkNo: '',
    // 因子名称
    materialNo: '',
    // 排放源
    sourceNo: ''
  })

  // 搜索配置
  const searchOptions: any[] = markRaw([
    {
      name: '能源/燃料',
      field: 'materialNo',
      type: 'select',
      remoteMethod: (query: string): Promise<any> => {
        console.log('remoteMethod', query)
        return getMaterialListDrop(query).then((res) => {
          console.log('res', res)
          const ans = res.map((item) => {
            return {
              label: `${item.materialName}-${item.materialUnit}`,
              value: item.materialNo
            }
          })
          return ans
        })
      }
    },
    {
      name: '排放源',
      field: 'sourceNo',
      type: 'cascader',
      filterable: true,
      loadData: (): Promise<any> => {
        return getEmissionOptions().then((res) => {
          console.log('res', res)
          const sourceOptions = res.map((item) => {
            const children = item.emissionSource.map((ite) => {
              return {
                label: ite.name,
                value: ite.sourceNo ////ite.sourceNo
              }
            })
            return {
              label: emissionTypeEnum[item.type],
              value: emissionTypeEnum[item.type], //item.type
              children
            }
          })
          return sourceOptions
        })
      }
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    selection: false,
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetRelationListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getRelationList(params)
  }

  // 处理参数
  const handleParams = (): GetRelationListParams => {
    // 参数
    let params: GetRelationListParams = {
      frameworkNo: searchForm.frameworkNo,
      materialNo: searchForm.materialNo,
      // 排放源
      sourceNo: searchForm.sourceNo
    }
    params = removeUndefinedParams(params)
    return params
  }

  // 表格每列配置
  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '关联关系编号',
      width: '115',
      field: 'relationNo',
      slot: 'relationNo',
      overflow: false,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '排放源/目录',
      minWidth: '150',
      field: 'directory',
      slot: 'directory',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '能源/燃料',
      field: 'material',
      slot: 'material',
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: `因子 (${CarbonInventoryStandardsMap.ISO})`,
      slot: 'ISO14064',
      field: 'ISO14064',
      overflow: false,
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: `因子 (${CarbonInventoryStandardsMap.GB})`,
      slot: 'GBT32150',
      field: 'GBT32150',
      overflow: false,
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: `因子 (${CarbonInventoryStandardsMap.GHG})`,
      slot: 'GHGProtocol',
      field: 'GHGProtocol',
      overflow: false,
      minWidth: '150',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'op',
      width: '110',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
    // {
    //   title: '登录邮箱',
    //   field: 'username',
    //   width: '200',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    // {
    //   title: '手机号',
    //   field: 'phone',
    //   width: '160',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    // {
    //   title: '用户角色',
    //   field: 'role',
    //   slot: 'roleSlot',
    //   minWidth: '210',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    // {
    //   title: '创建人/创建时间',
    //   field: 'createBy',
    //   slot: 'creatorSlot',
    //   width: '160',
    //   columnSettingConfig: {
    //     enabled: true
    //   }
    // },
    // {
    //   key: 'op',
    //   title: '操作',
    //   field: 'op',
    //   slot: 'op',
    //   width: '180',
    //   fixed: 'right',
    //   columnSettingConfig: {
    //     enabled: true,
    //     type: ColumnSettingTypeEnum.FIXED
    //   }
    // }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
